#define _CRT_SECURE_NO_WARNINGS
#include "stack.h"
void StackInit(Stack* ps)
{
	assert(ps);
	ps->capacity = ps->top=ps->number= 0;
	ps->arr = NULL;
}
void StackDestroy(Stack* ps)
{
	assert(ps);
	if (ps->arr)
		free(ps->arr);
	ps->arr = NULL;
	ps->capacity = ps->top =ps->number= 0;
}
void StackPush(Stack* ps, STDataType x)
{
	assert(ps);
	if (ps->capacity == ps->top)
	{
		int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		STDataType* tmp = (STDataType*)realloc(ps->arr, newcapacity * sizeof(STDataType));
		if (tmp == NULL)
		{
			perror("realloc fail");
			exit(1);
		}
		ps->arr = tmp;
		ps->capacity = newcapacity;
	}
	ps->arr[ps->top++] = x;
	ps->number++;
}
bool StackEmpty(Stack* ps)
{
	assert(ps);
	return ps->top == 0;
}
void StackPop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	ps->top--;
	ps->number--;
}
STDataType StackTop(Stack* ps)
{
	assert(ps);
	assert(!StackEmpty(ps));
	return ps->arr[ps->top-1];
}
int StackSize(Stack* ps)
{
	assert(ps);
	return ps->number;
}
